Set = {}
require "scripts.少年光头.Helper.hlua.henv"
require "scripts.少年光头.Helper.hlua.hcamera"
require "scripts.少年光头.Helper.hlua.iteam"
---关闭原版血条
function Set.Hp()
    cj.EnablePreSelect(false, false)
    return Set
end

---重写复活
---0 不复活 1 自定义原地复活
function Set.heroReborn(flag)
    --不复活
    if flag == 0 then
        --原地复活
        hevent_default_actions.hero.reborn = function(u, rebornSec)
            --不复活
        end
    elseif flag == 1 then
        hevent_default_actions.hero.reborn = function(u, rebornSec)
            local x = hunit.x(u)
            local y = hunit.y(u)
            if (rebornSec > 0) then
                local p = hunit.getOwner(u)
                htexture.mark(htexture.DEFAULT_MARKS.DREAM, rebornSec, p, 255, 0, 0)
                local ghost =
                    hunit.create(
                    {
                        register = false,
                        whichPlayer = hplayer.player_passive,
                        id = hunit.getId(u),
                        x = x,
                        y = y,
                        facing = 270,
                        height = 40,
                        modelScale = 0.9,
                        opacity = 0.6,
                        isShadow = true,
                        isInvulnerable = true,
                        during = rebornSec
                    }
                )
                hunit.setColor(ghost, hplayer.index(p))
                hunit.create(
                    {
                        register = false,
                        whichPlayer = p,
                        id = HL_ID.hero_death_token,
                        x = x,
                        y = y,
                        opacity = 0.8,
                        isShadow = true,
                        isInvulnerable = true,
                        during = rebornSec,
                        timeScale = 10 / rebornSec
                    }
                )
            end
            hhero.reborn(u, rebornSec, 1, x, y, true)
        end
    end
    return Set
end

--- 重写增加单位的当前生命
function Set.addCurLife()
    hunit.addCurLife = function(u, val)
        local cure = 1 + 0.01 * (hattribute.get(u, "cure"))
        val = math.round(val * cure, 2)
        if (val > 0) then
            hunit.setCurLife(u, hunit.getCurLife(u) + val)
            val = math.ceil(val)
            if val > 0 then
                --回复漂浮文字
                if hunit.getCurLife(u) ~= hunit.getMaxLife(u) then
                    htextTag.model({msg = "回血+" .. val, whichUnit = u, red = 0, green = 255, blue = 0})
                end
            end
        end
    end
    return Set
end

---重写单位升级
function Set.hero_levelUp(HoreS)
    if HoreS == nil then
        return print_err("单位升级 未指定HoreS")
    end
    hevent_default_actions.hero.levelUp =
        cj.Condition(
        function()
            local u = cj.GetTriggerUnit()
            --提升多少级
            local diffLv = cj.GetHeroLevel(u) - hhero.getPrevLevel(u)

            --判断是否升级
            if (diffLv < 1) then
                return
            end
            hhero.setPrevLevel(u, cj.GetHeroLevel(u))
            -- @触发升级事件
            hevent.triggerEvent(
                u,
                CONST_EVENT.levelUp,
                {
                    triggerUnit = u,
                    value = diffLv
                }
            )
            --单位名字
            local Name = hunit.getName(u)
            --echo(cj.GetHeroAgi(u, false))

            --每级提升
            for i = 1, diffLv, 1 do
                -- 重读部分属性（因为有些属性在物编可升级提升）
                for index, value in ipairs(HoreS) do
                    if value.Name == Name then
                        hattribute.set(
                            u,
                            0,
                            {
                                --白字力量
                                str_white = "=" .. cj.GetHeroStr(u, false),
                                --白字敏捷
                                agi_white = "=" .. cj.GetHeroAgi(u, false),
                                --白字智力
                                int_white = "=" .. cj.GetHeroInt(u, false),
                                --生命值
                                life = "+" .. value._diyShux.life,
                                --生命恢复成长
                                life_back = "+" .. (value._diyShux.life_back or 0),
                                mana = "+" .. (value._diyShux.mana or 0),
                                move = "+" .. (value._diyShux.move or 0),
                                defend = "+" .. (value._diyShux.defend or 0),
                                defend_white = "+" .. (value._diyShux.defend_white or 0),
                                defend_green = "+" .. (value._diyShux.defend_green or 0),
                                attack_speed = "+" .. (value._diyShux.attack_speed or 0),
                                attack_space = "+" .. (value._diyShux.attack_space or 0),
                                attack_space_origin = "+" .. (value._diyShux.attack_space_origin or 0),
                                attack = "+" .. (value._diyShux.attack or 0),
                                attack_white = "+" .. (value._diyShux.attack_white or 0),
                                attack_green = "+" .. (value._diyShux.attack_green or 0),
                                attack_range = "+" .. (value._diyShux.attack_range or 0),
                                attack_range_acquire = "+" .. (value._diyShux.attack_range_acquire or 0),
                                sight = "+" .. (value._diyShux.sight or 0),
                                str = "+" .. (value._diyShux.str or 0),
                                agi = "+" .. (value._diyShux.agi or 0),
                                int = "+" .. (value._diyShux.int or 0),
                                str_green = "+" .. (value._diyShux.str_green or 0),
                                agi_green = "+" .. (value._diyShux.agi_green or 0),
                                int_green = "+" .. (value._diyShux.int_green or 0),
                                mana_back = "+" .. (value._diyShux.mana_back or 0),
                                avoid = "+" .. (value._diyShux.avoid or 0),
                                aim = "+" .. (value._diyShux.aim or 0),
                                punish = "+" .. (value._diyShux.punish or 0),
                                punish_current = "+" .. (value._diyShux.punish_current or 0),
                                hemophagia = "+" .. (value._diyShux.hemophagia or 0), --(%)
                                hemophagia_skill = "+" .. (value._diyShux.hemophagia_skill or 0), --(%)
                                siphon = "+" .. (value._diyShux.siphon or 0), --(%)
                                siphon_skill = "+" .. (value._diyShux.siphon_skill or 0), --(%)
                                invincible = "+" .. (value._diyShux.invincible or 0), --(%)
                                weight = "+" .. (value._diyShux.weight or 0),
                                weight_current = "+" .. (value._diyShux.weight_current or 0),
                                damage_extent = "+" .. (value._diyShux.damage_extent or 0), --(%)
                                damage_reduction = "+" .. (value._diyShux.damage_reduction or 0),
                                --(固定)
                                damage_decrease = "+" .. (value._diyShux.damage_decrease or 0), --(%)
                                damage_rebound = "+" .. (value._diyShux.damage_rebound or 0), --(%)
                                damage_rebound_oppose = "+" .. (value._diyShux.damage_rebound_oppose or 0),
                                cure = "+" .. (value._diyShux.cure or 0), --(%)
                                reborn = "+" .. (value._diyShux.reborn or 0),
                                knocking_odds = "+" .. (value._diyShux.knocking_odds or 0),
                                knocking_extent = "+" .. (value._diyShux.knocking_extent or 0), --(%)
                                knocking_oppose = "+" .. (value._diyShux.knocking_oppose or 0),
                                hemophagia_oppose = "+" .. (value._diyShux.hemophagia_oppose or 0),
                                hemophagia_skill_oppose = "+" .. (value._diyShux.hemophagia_skill_oppose or 0),
                                siphon_oppose = "+" .. (value._diyShux.siphon_oppose or 0),
                                siphon_skill_oppose = "+" .. (value._diyShux.siphon_skill_oppose or 0),
                                buff_oppose = "+" .. (value._diyShux.buff_oppose or 0),
                                debuff_oppose = "+" .. (value._diyShux.debuff_oppose or 0),
                                split_oppose = "+" .. (value._diyShux.split_oppose or 0),
                                punish_oppose = "+" .. (value._diyShux.punish_oppose or 0),
                                swim_oppose = "+" .. (value._diyShux.swim_oppose or 0),
                                broken_oppose = "+" .. (value._diyShux.broken_oppose or 0),
                                silent_oppose = "+" .. (value._diyShux.silent_oppose or 0),
                                unarm_oppose = "+" .. (value._diyShux.unarm_oppose or 0),
                                fetter_oppose = "+" .. (value._diyShux.fetter_oppose or 0),
                                bomb_oppose = "+" .. (value._diyShux.bomb_oppose or 0),
                                lightning_chain_oppose = "+" .. (value._diyShux.lightning_chain_oppose or 0),
                                crack_fly_oppose = "+" .. (value._diyShux.crack_fly_oppose or 0)
                            }
                        )
                        break
                    end
                end
            end

            if (his.enablePunish(u)) then
                hmonitor.listen(CONST_MONITOR.PUNISH, u)
            end
        end
    )
    return Set
end


--触发事件
--模型伤害漂浮(绑定单位)
function Set.onDamageModel(whichUnit)
    hevent.onDamage(
        whichUnit,
        function(evtData)
            htextTag.model(
                {msg = "-" .. math.ceil(evtData.damage), whichUnit = evtData.targetUnit, red = 255, green = 0, blue = 0}
            )
        end
    )
    return Set
end








